package ru.spb.hibissscus.client;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpHost;
import org.apache.http.NameValuePair;
import org.apache.http.client.utils.URIUtils;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.entity.BufferedHttpEntity;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ru.spb.hibissscus.http.HttpMaster;

/**
 * Master for WotTankopedia
 */
public class WotTankopediaService {

    private static final Logger LOG = LoggerFactory
            .getLogger(WotTankopediaService.class);

    /**
     * Settings to HttpMaster
     */
    private String scheme;

    /**
     * Host
     */
    private String host;

    /**
     * Master to make the http conaction
     */
    private HttpMaster httpMaster;

    /**
     * Proxy
     */
    private HttpHost proxy;

    public WotTankopediaService() {
        setupDefaulValues();
    }

    private void setupDefaulValues() {
        this.scheme = "http";
        this.host = "worldoftanks.ru";
        this.proxy = null;
        this.httpMaster = new HttpMaster();
    }

    public void setProxy(HttpHost proxy) {
        this.proxy = proxy;
        setupProxy();
    }

    public void setProxy(String host, int port) {
        this.proxy = new HttpHost(host, port);
        setupProxy();
    }

    public HttpHost getProxy() {
        return proxy;
    }

    /**
     * Setup proxy settings
     */
    private void setupProxy() {
        getHttpMaster().setupProxy(proxy);
    }

    public HttpMaster getHttpMaster() {
        return httpMaster;
    }

    /**
     * Get words translation
     */
    public String getMainPage() {

        try {
            String path = "/encyclopedia/vehicles/";

            List<NameValuePair> qparams = new ArrayList<NameValuePair>();
            // qparams.add(new BasicNameValuePair("client", "t"));
            // qparams.add(new BasicNameValuePair("text", str));
            // qparams.add(new BasicNameValuePair("sl", languageFrom));
            // qparams.add(new BasicNameValuePair("tl", languageTo));

            URI uri = URIUtils.createURI(scheme, host, -1, path,
                    URLEncodedUtils.format(qparams, "UTF-8"), null);

            LOG.info("Url: {}", uri);

            // Response from Google Translation
            BufferedHttpEntity entity = getHttpMaster().getResponseEntity(
                    uri.toString());
            if (entity != null) {
                String s = EntityUtils.toString(entity);
                // lastTranslation = buildTranslation(s);
                // lastTranslation.setToLanguage(to);

                return s;
            } else
                return null;

        } catch (URISyntaxException e) {
            e.printStackTrace();
            LOG.error("Exception type: {}. message: {}", e.getClass(), e);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            LOG.error("Exception type: {}. message: {}", e.getClass(), e);
        } catch (IOException e) {
            e.printStackTrace();
            LOG.error("Exception type: {}. message: {}", e.getClass(), e);
        }

        return null;
    }

    private void getAdditionalInformation() {

    }

}
